package com.ljc.cjgl.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.repository.ClassRepository;

@Service
public class ClassService {
	@Autowired
	private ClassRepository classDAO;
	
	public List<TbClass> findAll(){

		return classDAO.findAll();
	}

	public Page<TbClass> findAll(Specification<TbClass> spec, PageRequest pageRequest) {
		return classDAO.findAll(spec, pageRequest);
	}

	public TbClass findById(Integer id) {
		Optional<TbClass> optional = classDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}

	public void save(TbClass model) {
		classDAO.save(model);
	}

	public Boolean deleteById(Integer id) {
		try {
			classDAO.deleteById(id);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public Boolean delete(List<TbClass> bjs) {
		try {
			for (TbClass c : bjs) {
				classDAO.deleteById(c.getId());
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<TbClass> findAll(Sort sort) {
		return classDAO.findAll(sort);
	}

	public List<TbClass> findNameLike(String bjMc) {
		return classDAO.findByNameLike("%"+bjMc+"%");
	}
}
